// pages/consult/consult.js
var api = require('../../utils/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,                   //初始页码
        pageSize: 10,
        keyword:'',
        apipath: api.Apipath,
        index1: 0,
        index2: 0,
        name: '',
        disease: '',


    },

    onLoad: function (options) {
      var that = this;
      if (wx.getStorageSync('userid')) {
        wx.request({
          url: api.Search,
          data: { keyword: that.data.keyword, page: that.data.page, pageSize: that.data.pageSize },
          method: 'GET',
          success: function (res) {
            that.setData({
              doctorsRec: res.data.data//医生推荐
            })
          },
        })

        wx.request({
          url: api.DoctorList,
          data: { site: wx.getStorageSync('role')},
          method: 'GET',
          success: function (res) {
            console.log(res);
            that.setData({
              doctorsList: res.data.data//专家姓名
            })
          },
        })
        wx.request({
          url: api.DiseaseList,
          data: { site: wx.getStorageSync('role') },
          method: 'GET',
          success: function (res) {
            console.log(res);
            that.setData({
              diseaseList: res.data.data//疾病名称
            })
          },
        })
      } else {
        wx.navigateTo({
          url: '/pages/signin/signin',
        })
      }
    },
    bindPickerDoctor: function (e) {
      var that = this;
      if(e.detail.value==0){
        var name = '';
      }else{
        var name = that.data.doctorsList[e.detail.value];
      }
      console.log(name);
      wx.request({
        url: api.ConsultList,
        data: { name: name, disease: that.data.disease, page: that.data.page, pageSize: that.data.pageSize },
        method: 'GET',
        success: function (res) {
          console.log(res);
          that.setData({
            doctorsRec: res.data.data,
            index1: e.detail.value,
            name: name
          })
        },
      })
    },
    bindPickerDisease: function (e) {
      var that = this;
      if (e.detail.value == 0) {
        var disease = '';
      } else {
        var disease = that.data.diseaseList[e.detail.value];
      }
      console.log(that.data.diseaseList[e.detail.value]);
      wx.request({
        url: api.ConsultList,
        data: { disease: disease, name: that.data.name, page: that.data.page, pageSize: that.data.pageSize },
        method: 'GET',
        success: function (res) {
          console.log(res);
          if(res.data.code==200){
            that.setData({
              doctorsRec: res.data.data,
              index2: e.detail.value,
              disease: disease
            })
          }else{
            wx.showToast({
              title: '搜索内容不存在',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              doctorsRec: '',
              index2: e.detail.value,
              disease: disease
            })
          }
          
        },
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})